﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eService.BLL;
using HLVControl.Grid;
using HLVControl.Grid.Data;
using APP.QUANLY.Common;
using app.Common;
namespace eService.Presentation
{
    public partial class usrcDanhMuc_NguoiDung : UserControl
    {
        EnumNameForm.ThaoTac thaotac;
        ExcutePHANQUYEN exePQ;
        ExcuteUSER exeND;

        public usrcDanhMuc_NguoiDung()
        {
            InitializeComponent();
            LoadAll();
        }

        protected virtual void LoadAll()
        {
            LoadConnection();
            LoadUI();
        }
        private void LoadConnection()
        {
            ConnectionProvider.LoadAllConnection();
        }
        private void LoadUI()
        {
            exePQ = new ExcutePHANQUYEN();
            exeND = new ExcuteUSER();
            thaotac = EnumNameForm.ThaoTac.macdinh;
            ControlUI(thaotac);
            UIHelper.LoadFocus(txtTimKiem);
        }
        private void ControlUI(EnumNameForm.ThaoTac thaotac)
        {
            if (thaotac == EnumNameForm.ThaoTac.them || thaotac == EnumNameForm.ThaoTac.sua)
            {
                SetStateButton(false);
                if (thaotac == EnumNameForm.ThaoTac.them)
                    LoadValuesToUI(null);
                SetTextReadOnly(false);
                UIHelper.LoadFocus(txtTen);
            }
            if (thaotac == EnumNameForm.ThaoTac.macdinh || thaotac == EnumNameForm.ThaoTac.xoa)
            {
                SetStateButton(true);
                LoadValuesToUI(null);
                SetTextReadOnly(true);

                //Load TreeView
                FillTreeView();
                treeview.RefreshGrouping();
                exePQ.LoadToListViewQuyenHan(lstQuyen);

                txtTimKiem.Text = "";
                UIHelper.LoadFocus(txtTimKiem);
            }
        }

        private void SetStateButton(bool IsEnabled)
        {
            btnSua.Enabled = IsEnabled;
            btnThem.Enabled = IsEnabled;
            btnXoa.Enabled = IsEnabled;
            btnXacNhan.Visible = !IsEnabled;
            btnHuy.Visible = !IsEnabled;
            treeview.Enabled = IsEnabled;
            btnTimKiem.Enabled = IsEnabled;
        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtTen.ReadOnly = IsReadOnly;
            txtMatKhau.ReadOnly = IsReadOnly;
            txtNhapLaiMatKhau.ReadOnly = IsReadOnly;
            txtTenDangNhap.ReadOnly = IsReadOnly;
            lstQuyen.Enabled = !IsReadOnly;
        }
        private void LoadValuesToUI(USER obj)
        {
            if (obj == null)
            {
                obj = new USER();
                obj.UserName = "";
                obj.Pass = "";
                obj.GID = 0;
                obj.Status = 1;
                obj.Create = DateTime.Now;
            }

            txtTen.Text = obj.FullName;
            txtTenDangNhap.Text = obj.UserName;
            if (obj.Pass != "")
            {
                txtMatKhau.Text = TCEncryption.ToDecrypt(obj.Pass);
                txtNhapLaiMatKhau.Text = TCEncryption.ToDecrypt(obj.Pass);
            }
            else
            {
                txtMatKhau.Text = "";
                txtNhapLaiMatKhau.Text = "";
            }
            if (txtTenDangNhap.Text != "")
            {
                List<USERPERDETAIL> listQH = exePQ.GetAllBy(txtTenDangNhap.Text);
                exePQ.LoadQuyenHan(lstQuyen, listQH);
            }
        }
        private void FillTreeView()
        {
            List<USER> data = exeND.GetAll();
            exeND.LoadToTreeview(treeview, data);
        }
        private bool XuLy(EnumNameForm.ThaoTac thaotac)
        {
            var flag = false;
            try
            {
                if (txtMatKhau.Text != txtNhapLaiMatKhau.Text)
                {
                    MessageBox.Show("Mật khẩu nhập lại không giống!\nVui lòng nhập lại.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtNhapLaiMatKhau.Text = "";
                    UIHelper.LoadFocus(txtNhapLaiMatKhau);
                    return false;
                }
                string MatKhau = TCEncryption.ToEncrypt(txtMatKhau.Text);
                USER obj = exeND.LoadValues(txtTen.Text, txtTenDangNhap.Text, MatKhau, int.Parse("1"));
                flag = exeND.KQ;
                if (flag)
                {
                    flag = false;
                    switch (thaotac)
                    {
                        case EnumNameForm.ThaoTac.them:
                            flag = exeND.Add(obj);
                            if (flag)
                            {
                                foreach (ListViewItem item in lstQuyen.Items)
                                {
                                    if (item.Checked)
                                    {
                                        int MaQuyenHan = int.Parse(item.Text);
                                        USERPERDETAIL objPQ = exePQ.LoadValues(0, obj.UserName, MaQuyenHan);
                                        flag = exePQ.Add(objPQ);
                                    }
                                }
                            }
                            break;
                        case EnumNameForm.ThaoTac.sua:
                            flag = exeND.UpDate(obj);
                            if (flag)
                            {
                                List<USERPERDETAIL> list = exePQ.GetAllBy(obj.UserName);
                                foreach (ListViewItem item in lstQuyen.Items)
                                {
                                    int MaQuyenHan = int.Parse(item.Text);
                                    USERPERDETAIL objPQ = list.Find(c => c.PerID == MaQuyenHan);
                                    if (item.Checked)
                                    {
                                        if (objPQ == null)
                                        {
                                            objPQ = exePQ.LoadValues(0, obj.UserName, MaQuyenHan);
                                            flag = exePQ.Add(objPQ);
                                        }
                                    }
                                    else
                                    {
                                        if (objPQ != null) flag = exePQ.Delete(objPQ);
                                    }
                                }
                            }
                            break;
                        case EnumNameForm.ThaoTac.xoa:
                            flag = exeND.Delete(obj);
                            break;
                    }
                    flag = exeND.KQ;
                }
                MessageBox.Show(exeND.ErrMsg, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        private void button_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();
            if ((_thaotac == "sua" && txtTenDangNhap.Text == "") || (_thaotac == "xoa" && txtTenDangNhap.Text == ""))
            {
                MessageBox.Show("Vui lòng chọn người dùng!");
                return;
            }
            switch (_thaotac)
            {
                case "them":
                    thaotac = EnumNameForm.ThaoTac.them;
                    break;
                case "sua":
                    thaotac = EnumNameForm.ThaoTac.sua;
                    break;
                case "huy":
                    thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xoa":
                    KQ = XuLy(EnumNameForm.ThaoTac.xoa);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xacnhan":
                    KQ = XuLy(thaotac);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
            }
            ControlUI(thaotac);
        }
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            SearchProvider.SmartSearch(treeview, txtTimKiem.Text.Trim(), new List<string> {});
        }

        private void treeview_AfterSelectionChange(object sender, HLVControl.Grid.Events.TreeListElementEventArgs e)
        {
            try
            {
                TreeListRow firstSelected = treeview.SelectedElements[0] as TreeListRow;
                string username = firstSelected.Cells["colUserName"].Value.ToString();
                USER obj = exeND.GetByID(username);
                if (obj == null) return;
                LoadValuesToUI(obj);
            }
            catch
            {

            }
        }
        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (txt.Name == txtTen.Name)
                UIHelper.txt_KeyPress(sender, txtTenDangNhap, e);
            if (txt.Name == txtTenDangNhap.Name)
                UIHelper.txt_KeyPress(sender, txtMatKhau, e);
            if (txt.Name == txtMatKhau.Name)
                UIHelper.txt_KeyPress(sender, txtNhapLaiMatKhau, e);
            if (txt.Name == txtNhapLaiMatKhau.Name)
                UIHelper.txt_KeyPress(sender, btnXacNhan, e);
            if (txt.Name == txtTimKiem.Name)
                UIHelper.txt_KeyPress(sender, btnTimKiem, e);
        }
    }
}
